﻿
using CGT.API.Common;
using CGT.API.Models;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CGT.API.Controllers
{
    /// <summary>
    /// 消息
    /// </summary>
    public class MessageController : BaseApiController
    {
        /// <summary>
        /// 分页查询信息
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="messageType">状态</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页大小</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<List<MessageModel>> GetMessageTypePage(int userID, int messageType, int pageIndex, int pageSize)
        {
            ResultData<List<MessageModel>> rData = new ResultData<List<MessageModel>>();
            int pageCount;
            DataTable dt = MessageOperate.GetMessageTypePage(userID, messageType, pageIndex, pageSize, out pageCount);
            List<MessageModel> models = new List<MessageModel>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    MessageModel model = new MessageModel();
                    model.IsRead = EConvert.ToInt(row["IsRead"]);
                    model.MessageID = EConvert.ToLong(row["MessageID"]);
                    model.MessageType = EConvert.ToInt(row["MessageType"]);
                    model.SendDate = EConvert.ToDateTime(row["SendDate"]).ToString("MM-dd HH:mm");
                    model.MessageTitle = EConvert.ToString(row["MessageTitle"]);
                    model.MessageDesc = EConvert.ToString(row["MessageDesc"]);
                    model.MessagePoolID = EConvert.ToInt(row["MessagePoolID"]);

                    models.Add(model);
                }
            }
            rData.data = models;
            rData.message = pageCount.ToString();
            return rData;
        }


        /// <summary>
        /// 根据主键查询信息
        /// </summary>
        /// <param name="messageID">消息主键</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<MessageInfo> GetMessageInfo(long messageID)
        {
            ResultData<MessageInfo> rData = new ResultData<MessageInfo>();
            MessageInfo model = MessageOperate.GetByID(messageID);

            rData.data = model;
            return rData;
        }

        /// <summary>
        /// 设置消息已读
        /// </summary>
        /// <param name="messageID">消息主键</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<string> SetMessageRead(long messageID)
        {
            ResultData<string> rData = new ResultData<string>();
            MessageOperate.SetMessageRead(messageID);
            return rData;
        }

        /// <summary>
        /// 删除预约
        /// </summary>
        /// <param name="model">预约</param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<string> DeleteMessage([FromBody]DeleteModel model)
        {
            ResultData<string> rData = new ResultData<string>();
            MessagePoolInfo meg = MessagePoolOperate.QuerySingle(a => a.MessagePoolID == model.Id && a.IsActive == 1);
            if (meg == null)
            {
                rData.status = -1;
                rData.message = "消息已经删除.";
            }
            else
            {
                meg.IsActive = 0;
                MessagePoolOperate.Update(meg);
            }
            return rData;
        }

        /// <summary>
        /// 获取未读消息
        /// </summary>
        /// <param name="userID">用户主键</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<List<PushMessageModel>> GetMessagePush(int userID)
        {

            ResultData<List<PushMessageModel>> rData = new ResultData<List<PushMessageModel>>();

            DataTable dt = MessageOperate.GetMessagePush(userID);
            List<PushMessageModel> models = new List<PushMessageModel>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    PushMessageModel model = new PushMessageModel();
                    model.MessageID = EConvert.ToInt(row["MessageID"]);
                    model.MessageTitle = EConvert.ToString(row["MessageTitle"]);
                    model.MessageDesc = EConvert.ToString(row["MessageDesc"]);
                    models.Add(model);
                }
            }
            rData.data = models;
            return rData;
        }

        /// <summary>
        /// 根据类型分组汇总
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<List<MessageTotalModel>> GetMessageTotalType(int userID)
        {
            ResultData<List<MessageTotalModel>> rData = new ResultData<List<MessageTotalModel>>();
            DataTable dt = MessageOperate.GetMessageTotalType(userID);
            List<MessageTotalModel> models = new List<MessageTotalModel>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    MessageTotalModel model = new MessageTotalModel();
                    model.CT = EConvert.ToInt(row["CT"]);
                    model.MessageType = EConvert.ToInt(row["MessageType"]);
                    models.Add(model);
                }

            }
            rData.data = models;
            return rData;
        }

        /// <summary>
        /// 查询未读消息汇总
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<int> GetMessageTotal(int userID)
        {
            ResultData<int> rData = new ResultData<int>();
            rData.data = MessageOperate.GetMessageTotal(userID);
            return rData;
        }
    }
}
